<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Validator;
use DateTimeInterface;

class BaseModel extends Model
{

    public function __construct()
    {
        parent::__construct();
    }

    // 修复时区晚8个小时的问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function formatPaged($page, $size, $total)
    {
        return [
            'total' => $total,
            'page' => $page,
            'size' => $size,
            'more' => ($total > $page * $size) ? 1 : 0
        ];
    }

    public static function formatBody(array $data = [], $message='')
    {
        $data['code'] = 0;
        $data['msg'] = !empty($message) ? $message : config('error.0');
        return $data;
    }

    public static function formatError($code, $message = null, array $data=[])
    {
        $body['code'] = $code;
        $body['msg'] = !empty($message) ? $message : config('error.'.$code);
        if ($data)
        {
            $body['data'] = $data;
        }
        return $body;
    }

}
